/*publish time:2011-08-03 14:30:05*/
(function (A) {
    A.fn.lazyload = function (F) {
        var G = {placeholder:"ky_load.gif", threshold:50, container:window};
        if (F) {
            A.extend(G, F)
        }
        var I = this, C = A('textarea[class="data-lazyload"]');
        this.each(function () {
            H(this)
        });
        function H(J) {
            var K = A(J);
            if (K.attr("ky_src")) {
                K.attr("src", G.placeholder)
            }
        }

        function E() {
            C.each(function () {
                if (D(this, G.container)) {
                    var L = A(this).parent(), K = A(this).val();
                    L[0].innerHTML = K;
                    var M = A("img", L[0]);
                    M.each(function () {
                        H(this)
                    });
                    this.loaded = true;
                    var J = A.grep(C, function (N) {
                        return !N.loaded
                    });
                    C = A(J);
                    A.merge(I, A.makeArray(M));
                    A(G.container).trigger("scroll")
                }
            })
        }

        function B() {
            I.each(function () {
                var K = A(this);
                if (K.attr("ky_src") && D(this, G.container)) {
                    K.hide().attr("src", K.attr("ky_src")).fadeIn();
                    K.removeAttr("ky_src");
                    K.loaded = true;
                    var J = A.grep(I, function (L) {
                        return !L.loaded
                    });
                    I = A(J)
                }
            })
        }

        A(G.container).bind("scroll resize", function () {
            E();
            B()
        });
        function D(T, S) {
            var L = A(T), J = A(S), U = J.width(), P = J.height(), N = J.scrollLeft(), V = J.scrollTop(), K = L.width(), R = L.height(), O = L.offset(), Q = O.left, M = O.top;
            return(M + R * G.threshold / 100) <= (P + V) && (Q + K * G.threshold / 100) <= (U + N)
        }

        A(G.container).trigger("scroll")
    }
})(jQuery);